package com.cgbl.service.vm

import android.app.Application
import android.text.TextUtils
import androidx.databinding.ObservableBoolean
import androidx.databinding.ObservableField
import api.WorkServiceFactory
import com.robot.base.base.vm.GLBaseViewModel
import com.robot.base.common.api.AbstractViewModelSubscriber
import com.robot.base.configs.AppConstants
import com.robot.base.model.BaseResponse
import com.robot.base.model.MemberAdapterBean
import com.robot.base.model.TagListBean
import com.robot.base.model.TeamListBean
import com.robot.base.util.LiveDataEventManager
import com.robot.base.util.ToastUtils
import com.robot.base.util.arouter.ARouterUtil
import me.goldze.mvvmhabit.binding.command.BindingAction
import me.goldze.mvvmhabit.binding.command.BindingCommand
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent
import java.util.Observable

class EditTagViewModel(application: Application) : GLBaseViewModel(application) {

    var id = ""

    val uc = UIChangeEvent()

    val memberIdList = arrayListOf<String>()

    val saveEnableField = ObservableBoolean(false)

    val labelTitle = ObservableField<String>()

    val saveCommand = BindingCommand<Any>(BindingAction {
        updateLabel()
    })

    val deleteCommand = BindingCommand<Any>(BindingAction {
        if (TextUtils.isEmpty(id)) {
            //创建
            createLabel()
        } else {
            //删除
            deleteLabel()
        }
    })

    class UIChangeEvent {

        val labelMemberEvent = SingleLiveEvent<MutableList<MemberAdapterBean>>()

    }


    fun fetchLabelProfile(id: String) {
        this.id = id
        if (TextUtils.isEmpty(id)) return
        WorkServiceFactory.getLabelById(id)
            .subscribe(object : AbstractViewModelSubscriber<BaseResponse<TagListBean>>(this) {
                override fun onNext(t: BaseResponse<TagListBean>) {
                    labelTitle.set(t.data.labelName)
                    uc.labelMemberEvent.postValue(t.data.accountDOS as MutableList<MemberAdapterBean>?)
                }
            })
    }

    fun createLabel() {
        val labelName = labelTitle.get() ?: ""
        val params = mapOf("labelName" to labelName, "teamAccountIds" to memberIdList)
        WorkServiceFactory.createLabel(params)
            .subscribe(object : AbstractViewModelSubscriber<BaseResponse<String>>(this) {
                override fun onNext(t: BaseResponse<String>) {
                    ToastUtils.showShort("创建成功!")
                    LiveDataEventManager.postRefreshLabelEvent()
                    finish()
                }
            })
    }

    fun updateLabel() {
        val labelName = labelTitle.get() ?: ""
        val params = mapOf("labelName" to labelName, "teamAccountIds" to memberIdList,"id" to id)
        if (TextUtils.isEmpty(id)) return
        WorkServiceFactory.updateLabel(params)
            .subscribe(object : AbstractViewModelSubscriber<BaseResponse<String>>(this) {
                override fun onNext(t: BaseResponse<String>) {
                    ToastUtils.showShort("修改成功!")
                    LiveDataEventManager.postRefreshLabelEvent()
                    finish()
                }
            })
    }

    fun deleteLabel() {
        if (TextUtils.isEmpty(id)) return
        WorkServiceFactory.deletesLabel(id)
            .subscribe(object : AbstractViewModelSubscriber<BaseResponse<String>>(this) {
                override fun onNext(t: BaseResponse<String>) {
                    ToastUtils.showShort("删除成功!")
                    LiveDataEventManager.postRefreshLabelEvent()
                    finish()
                }
            })
    }
}